/** @file MovePlayerCommand.h
    @class MovePlayerCommand
    @brief Command that moves a player according to physics and tells if player wants to jump
*/
#ifndef __MovePlayerCommand_h_
#define __MovePlayerCommand_h_

#include "Command.h"
class MovePlayerCommand : public Command {
public:
	/**
	* @brief The constructor
	*/
	MovePlayerCommand (float x, float y, float dT, bool wantJump, unsigned int playerId)
		: Command(playerId), m_dX(x), m_dY(y), m_deltaTime(dT), m_wantJump(wantJump) {}
		
	/**
	* @brief The empty constructor
	*/
	MovePlayerCommand() {}
		
	/**
	* @brief The destructor
	*/
	virtual ~MovePlayerCommand (void) {}
	
	/**
	* @brief Executs the command on argument model
	*/
	virtual bool execute(Model *model) {		
		GameObject *gameObject = model->getByID(getPlayerId());
		if ( gameObject == NULL) {
			return false;
		} else {
			PlayerAvatar* pa = dynamic_cast<PlayerAvatar*>(gameObject);
			pa->updateCharacter(m_deltaTime, m_dX, m_dY, m_wantJump);
			return true;
		}
	}
	
	/**
	* @brief Saves the command to a output file stream
	*/
	virtual void save(ofstream &ofs) {
		int i = MOVEPLAYERCOMMAND;
		ofs.write((char*) &i, sizeof(int));
		Command::save(ofs);
		ofs.write((char*) &m_deltaTime, sizeof(float));
		ofs.write((char*) &m_dX, sizeof(float));
		ofs.write((char*) &m_dY, sizeof(float));
		ofs.write((char*) &m_wantJump, sizeof(bool));
  }

	/**
	* @brief Loads a command from a input file stream
	*/
  virtual void load(ifstream &ifs) {
		Command::load(ifs);
		ifs.read((char*) &m_deltaTime, sizeof(float));
		ifs.read((char*) &m_dX, sizeof(float));
		ifs.read((char*) &m_dY, sizeof(float));
		ifs.read((char*) &m_wantJump, sizeof(bool));
  }

	/**
	* @brief Saves the command to a bit stream
	*/
  virtual void save(RakNet::BitStream &ofs) {
		int i = MOVEPLAYERCOMMAND;
		ofs.Write((char*) &i, sizeof(int));
		Command::save(ofs);
		ofs.Write((char*) &m_deltaTime, sizeof(float));
		ofs.Write((char*) &m_dX, sizeof(float));
		ofs.Write((char*) &m_dY, sizeof(float));
		ofs.Write((char*) &m_wantJump, sizeof(bool));
  }

	/**
	* @brief Loads a command from a bit stream
	*/
  virtual void load(RakNet::BitStream &ifs) {
		Command::load(ifs);
		ifs.Read((char*) &m_deltaTime, sizeof(float));
		ifs.Read((char*) &m_dX, sizeof(float));
		ifs.Read((char*) &m_dY, sizeof(float));
		ifs.Read((char*) &m_wantJump, sizeof(bool));
  }

private:
	/**
	* @brief Movement in x direction
	*/
	float m_dX;
	
	/**
	* @brief Movement in y direction
	*/
	float m_dY;
	
	/**
	* @brief DeltaTime for the frame
	*/
	float m_deltaTime;
	
	/**
	* @brief If the player wants to jump
	*/
	bool  m_wantJump;
	
};

#endif